import { defineStore } from 'pinia'
import { reqLogout } from '@/API/user/index.js'
import { userLogOutReponseData } from '@/API/user/type.js'
import { useRouter } from 'vue-router'
import { useRoute } from 'vue-router'
import useUserStore from './useUser.js'

const useUserlogOut = defineStore('UserlogOut', () => {
  const UserStore = useUserStore()
  const $router = useRouter()
  const $route = useRoute()
  async function userlogOut() {
    const result: userLogOutReponseData = await reqLogout()
    console.log(result)

    if (result.code == 200) {
      await UserStore.Logout()
      localStorage.clear()
      localStorage.removeItem('token')
      $router.replace({ path: '/login', query: { redirect: $route.path } })
      console.log('登出')
      return 'ok'
    } else {
      return Promise.reject(new Error(result.message))
    }
  }

  return { userlogOut }
})
export default useUserlogOut
